/**
 * 学习绽放之花js
 * @param maxFraction1 最大分值
 * @param fractions1 每个类别的分值集合
 * @param colorList1 颜色集合
 * @param wordList1 类别名集合
 * @param logoName1 logo中的文字
 * @param num 花瓣数量
 * @param v 旋转速度(0-1之间)
 */
var createFlower = function (maxFraction1, fractions1, colorList1, wordList1, logoName1, logoImg, num, v) {
    canvas = $("#myCanvas")[0];
    console.log(canvas)
    ctx = canvas.getContext("2d");

    if(null == num || num < 5){
        flowerNum = 5;
    }else{
        flowerNum = num;
    }
    if(null == maxFraction1){
        maxFraction = [];
        for(let n =0 ; n < flowerNum ;n++){
            maxFraction.push(9)
        }
    }else{
        maxFraction = maxFraction1;
    }

    if(null == fractions1){
        fractions = [];
        for(let n =0 ; n < flowerNum ;n++){
            fractions.push(3)
        }
    }else{
        fractions = fractions1;
    }

    if(null == colorList1){
        let c = [
            {r:255,g:211,b:0},
            {r:129,g:202,b:85},
            {r:65,g:177,b:227},
            {r:219,g:135,b:212},
            {r:253,g:109,b:101}
        ];
        colorList = [];
        for(let n =0 ; n < flowerNum ;n++){
            colorList.push(c[n%5])
        }
    }else{
        colorList = colorList1;
    }

    // 初始文字设置
    if(null == wordList1){
        let t = [
            "",
            "",
            "",
            "",
            ""
        ];
        wordList = [];
        for(let n =0 ; n < flowerNum ;n++){
            wordList.push(t[n%5])
        }
    }else{
        wordList = wordList1;
    }

    img=null;
    if (null != logoImg) {
        img = logoImg;
    } else {
        img = new Image();
        img.src = "static/images/ysfx/ysfx_logo.png";
    }

    // 学习之花的初始角度
    if(null == v){
        v = 0.2
        anglea = 0.2;
    }else {
        anglea = v
    }

    // 每次绘制增加的角度
    // transAngle = 0.0001;

    if(null == logoName1){
        logoName = "";
    }else{
        logoName = logoName1;
    }

    timesRun = 0;

    // interval =setInterval("init();",60);
    timerFun();
}
function timerFun(){

    //要执行的操作

    init();

    // 运行指定次数停止
    if (timesRun >= 3600) {
            return ;
    }
    var timer=setTimeout(function(){

        timerFun();

        clearTimeout(timer);

    },80)

}

function init() {
    timesRun ++;
    anglea;
    ctx.clearRect(0,0,canvas.width,canvas.height);
    ctx.translate(canvas.width/2,canvas.height/2);
    ctx.rotate(anglea*Math.PI/180);
    ctx.translate(-canvas.width/2,-canvas.height/2);
    drawFlower(canvas,ctx,170,flowerNum,maxFraction,fractions,colorList,wordList,logoName);
}

function rgba(r,g,b,a) {
    return "rgba("+r+","+g+","+b+","+a+")";
}

/**
 *  画花
 * @param canvas
 * @param ctx
 * @param height 花瓣高度
 * @param num 花瓣数量
 * @param maxFraction 最高分
 * @param fractions 分数集合
 * @param colorList 花瓣颜色集合
 * wordList 每片花瓣上文字的集合
 * logoName 花中心logo的文字
 */
function drawFlower(canvas,ctx,height,num,maxFraction,fractions,colorList,wordList,logoName) {
    angle = 300/num;


    for (var i=0;i<num;i++){
        if (fractions[i]>maxFraction[i]){
            fractions[i] = maxFraction[i];
        } else if(fractions[i]<1){
            fractions[i] = 0;
        }
        ctx.translate(canvas.width/2,canvas.height/2);
        ctx.rotate((360/num)*Math.PI/180);
        ctx.translate(-canvas.width/2,-canvas.height/2);
        var r = colorList[i].r;
        var g = colorList[i].g;
        var b = colorList[i].b;
        // var fraction = random(1,6); // 分数
        // if(0 == fractions[i]){
        //     drawPetal(canvas,ctx,(height-50)*fractions[i]/maxFraction[i],angle,rgba(r,g,b,1),"");
        // }else{
            drawPetal(canvas,ctx,(height-50)*fractions[i]/maxFraction[i]+30,angle,rgba(r,g,b,1),"");
        // }
        drawPetal(canvas,ctx,height,angle,rgba(r,g,b,0.3),wordList[i]);

        // ctx.restore();
    }
        drawLogo(canvas,ctx,100,logoName);

}

/**
 * 画花中心的logo
 * @param canvas
 * @param ctx
 * @param height
 * @param word
 */
function drawLogo(canvas,ctx,height,word){
    // var img=document.getElementById("tulip");

    var x= canvas.width/2 - height/2;
    var y= canvas.height/2-height/2;
    ctx.drawImage(img,x,y,height,height);
    ctx.font="14px 黑体";
    ctx.fillStyle="#000";
    ctx.fillText(word,x+25, y+60);
}

/**
 *  画一片花瓣
 * @param canvas
 * @param ctx
 * @param height 高度
 * @param angle 角度
 * @param rgb 颜色
 * @param word 花瓣上的文字
 */
function drawPetal(canvas,ctx,height,angle,rgb,word) {
    var angle = angle/180*Math.PI;// 弧度制角度
    var x = Math.tan(angle) * height;
    var r = Math.tan(angle/4)*height;
    // 弧度起始点
    var startX = angle/2;
    var startY = angle/2;
    ctx.beginPath();
    // 花瓣顶点设置起始点
    ctx.moveTo(canvas.width/2,canvas.height/2);
    var startAngle = Math.PI-angle/2;
    var endAngle = 5*Math.PI/3;
    var sx = r - Math.cos(angle/2)*r;//小圆起始弧度x轴
    var sr = r;
    var sy = r/Math.tan(Math.PI/6);
    // 第一个弧度
    ctx.arc(canvas.width/2-r, canvas.height/2-height, r, startAngle,endAngle);
    // 顶部小弧度
    ctx.arc(canvas.width/2, canvas.height/2-height-sy, sr, 2*Math.PI/3,Math.PI/3,true);
    // 第三个弧度
    ctx.arc(canvas.width/2+r, canvas.height/2-height, r, 4*Math.PI/3,angle/2);
    ctx.closePath();
    ctx.fillStyle = rgb;
    ctx.lineWidth=2;
    ctx.fill();
    ctx.font="20px 黑体";
    ctx.fillStyle="#000";
    ctx.fillText(word,1.8*x/3,height/4);
}

/**
 *
 * @param min 最小值
 * @param max 最大值
 * @returns {*}返回最小值和最大值之间的随机值
 */
function random(min,max) {
    return Math.random()*(max - min)+min;
}

